/**
 * Created by 李柏霖
 * 2020/10/19 17:35
 */

package com.lbl.controller;


import com.lbl.VO.Result;
import com.lbl.domain.Employee;
import com.lbl.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping(path = "/listUI", method = {RequestMethod.GET, RequestMethod.POST})
    public String listUI(Model model) {
        System.out.println("employee....listUI.....run");
        //显示所有的person数据
        List<Employee> employeeList = employeeService.findAll();
        System.out.println(employeeList);
        model.addAttribute("employeeList", employeeList);
        return "list";
    }

    @ResponseBody
    @RequestMapping(path = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public Object list() {
        System.out.println("employee....list.....run");
        //显示所有的person数据
        List<Employee> employeeList = employeeService.findAll();
        System.out.println(employeeList);
        return Result.init(200, "查询成功", employeeList);
    }

    @RequestMapping(path = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object delete(Integer eid) {
        System.out.println("employee....delete.....run" + eid);
        //显示所有的person数据
        try {
            employeeService.deleteById(eid);
            return Result.init(200, "删除成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.init(-200, "删除失败", null);
        }
    }

    @ResponseBody
    @RequestMapping(path = "/updateUI", method = {RequestMethod.GET, RequestMethod.POST})
    public Object updateUI(Integer eid) {
        System.out.println("employee....updateUI.....run");
        //显示所有的person数据
        Employee employee = employeeService.findById(eid);
        return Result.init(200, "更新页面", employee);
    }

    @ResponseBody
    @RequestMapping(path = "/updateById", method = {RequestMethod.GET, RequestMethod.POST})
    public Object updateById(Employee employee) {
        System.out.println("employee....updateById.....run");
        System.out.println(employee);
        //显示所有的person数据
        try {
            employeeService.update(employee);
            return Result.init(200,"更新成功",null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.init(-200,"更新失败",null);
    }

    @ResponseBody
    @RequestMapping(path = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public Object add(Employee employee) {
        System.out.println("employee....add.....run");
        System.out.println(employee);
        //显示所有的person数据
        try {
            employeeService.save(employee);
            return Result.init(200,"添加成功",null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.init(-200,"添加失败",null);
    }

}
